<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import Edit from "views/productlist/freight/edit/index";
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  props: {
    // 使用者window实例 必定义
    appWindow: {
      type: Object,
      default: null,
    },
    // 配送模板列表
    templateOptions: {
      type: Array,
      default: [],
    },
    // 商品类型
    productType: {
      type: Number,
      default: 2,
    },
  },
  computed: {},

  data() {
    return {
      // 初始化状态
      initLoad: false,
      // 编辑类型
      editType: "",

      form: {
        // 是否开启商家配送 0否 1是
        isStoreDelivery: 1,
        isSelfDelivery: 1, //是否开启自提
        //    模板id
        freightTemplateId: "",
      },

      // 是否展开模板
      tableShow: true,
    };
  },

  methods: {
    getTemplate() {
      this.$emit("getTemplate");
    },

    // 商家运费模版
    validator(rule, value, callback) {
      if (this.productType == 2) {
        callback();
      } else if (this.form.isStoreDelivery == 0) {
        callback();
      } else {
        if (value == "") {
          callback(new Error("请选择商家运费模版"));
        } else {
          callback();
        }
      }
    },
    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },
    getData() {
      let _d = {
        isStoreDelivery: this.form.isStoreDelivery,
        isSelfDelivery: this.form.isSelfDelivery,
        freightTemplateId: this.form.freightTemplateId,
      };
      return _d;
    },
    async install(params_) {
      this.editType = params_.type;
      if (params_.datasource) {
        this.form.isStoreDelivery = params_.datasource.isStoreDelivery;
        this.form.isSelfDelivery = params_.datasource.isSelfDelivery;
        this.form.freightTemplateId = params_.datasource.freightTemplateId;
      }
      this.initLoad = true;
      this.returnData();
    },
    // 新增模板
    addTemplate() {
      Edit.init({
        mixinEdit_editType: "add",
        id: "",
        onSubmitSuccess: (res) => {
          this.getTemplate();
        },
      });
    },
    async uninstall() {
      this.$refs["editForm"].resetFields();
    },
    returnData() {
      this.$emit("change", {
        data: this.getData(),
        load: this.initLoad,
      });
    },
  },
  mounted() {},
  beforeCreate() {},
  watch: {
    form: {
      handler(newName, oldName) {
        this.returnData();
      },
      immediate: true,
      deep: true,
    },
  },
};
</script>